﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace template_dll
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        SqlConnection conn = new SqlConnection();
        public int rezult;                                  //- результат занесения в базу
        private DataTable dt_tree = new DataTable();        //- Для вывода каталога
        private DataTable dt_drug_tmp = new DataTable();    //- Для отображения на форме
        public SqlConnection sqlconn = new SqlConnection(@"Server=sdata2;Database=PharmOLAPDW;Integrated Security=True");

        private TreeView LoadTreeView(TreeView tree)
        {
            dt_tree = null;
            dt_tree = new DataTable();
            string sp_name = @"pas_BrandTreeSelDM";
            SqlCommand comm = new SqlCommand();
            comm.CommandType = CommandType.StoredProcedure;
            comm.Connection = sqlconn;
            comm.CommandText = sp_name;
            comm.Parameters.AddWithValue("@drugs", 0);
            comm.Parameters.AddWithValue("@dm", 1);
            if (sqlconn.State == ConnectionState.Closed) sqlconn.Open();
            dt_tree.Load(comm.ExecuteReader());

            TreeNode tn = new TreeNode();
            DataView dv = new DataView(dt_tree);
            DataTable dt_dis = dv.ToTable(true, "BrandName1");
            foreach (DataRow dr in dt_dis.Rows)
            {
                TreeNode tn1 = new TreeNode();
                tn1.Text = dr[0].ToString();
                treeView1.Nodes.Add(tn1);
            }
            foreach (TreeNode treenode in tree.Nodes)
            {
                string[] selparamval = new string[1];
                selparamval[0] = treenode.Text;
                TreeNodeLoad(2, selparamval, treenode, dt_tree);
            }

            return tree;
        }
        private TreeNode TreeNodeLoad(int col, string[] selparamval, TreeNode node, DataTable table)
        {
            if (col <= 6)
            {
                string[] param = new string[] { };
                string selparam = "";
                for (int i = 0; i < col; i++)
                {
                    if (i < 5)
                    {
                        Array.Resize(ref param, param.Length + 1);
                        param[param.Length - 1] = "BrandName" + (i + 1).ToString();
                        if (i < col - 1)
                        {
                            if (selparam != "") selparam += " and ";
                            selparam += param[i] + " = '" + selparamval[i] + "'";
                        }
                    }

                }
                string[] newselparam = selparamval;
                Array.Resize(ref newselparam, newselparam.Length + 1);
                foreach (DataRow dr in (new DataView(table)).ToTable(true, param).Select(selparam))
                {
                    if (col < 6)
                    {
                        if (dr[param[col - 1]].ToString() != "")
                        {
                            int ind = node.Nodes.Add(dr[param[col - 1]].ToString()).Index;
                            newselparam[newselparam.Length - 1] = node.Nodes[ind].Text;
                            TreeNodeLoad(col + 1, newselparam, node.Nodes[ind], table);
                        }
                        if (dr[param[col - 1]].ToString() == "")
                        {
                            string s = dt_tree.Select(selparam)[0]["BrandID"].ToString();
                            node.ToolTipText = dt_tree.Select(selparam)[0]["BrandID"].ToString();
                        }
                    }
                    else
                    {
                        node.ToolTipText = dt_tree.Select(selparam)[0]["BrandID"].ToString();
                    }
                }
            }
            return node;
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //- Назначение группы для вставки
            if (e.Node.Level == 1)
            {
                //- если выбрана группа первого уровня
                ((ChildForm)template_dll.ChildForm.cMain.Form1Control).pGroup = e.Node.Text;
            }
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            //- Выбор группы
            dt_tree.Clear();
            this.Text += " Загрузка каталога";
            LoadTreeView(treeView1);
            this.Text = Text.Replace(" Загрузка каталога", "");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //- Закрыть форму без выбора
            ((ChildForm)template_dll.ChildForm.cMain.Form1Control).pGroup = null;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //- Выбрать группу
            ((ChildForm)template_dll.ChildForm.cMain.Form1Control).refrData();
        }

        private void treeView1_NodeMouseClick_1(object sender, TreeNodeMouseClickEventArgs e)
        {
            //- Выбираем группу
            ((ChildForm)template_dll.ChildForm.cMain.Form1Control).pGroup = e.Node.Text;
        }
    }
}
